package cn.itcast.dao;

import java.io.InputStream;
import java.util.Properties;

/*
* 工厂模式: 通过反射创建实现Dao接口的对象
* 单例模式: 工厂往往单例模式设计
* */
public class DaoFactory {
    private static UserDao userDao = null;
    private static DaoFactory daoFactory = null;

    private DaoFactory()  {
        try {
            Properties properties = new Properties();
            InputStream in = DaoFactory.class.getClassLoader().getResourceAsStream("daoconfig");
            properties.load(in);

            String userDaoClass = properties.getProperty("UserDaoclass");
            Class clazz = Class.forName(userDaoClass);
            userDao = (UserDao) clazz.newInstance();

        }catch (Exception e){
            throw new ExceptionInInitializerError(e);
        }

    }

    public static DaoFactory getInstance(){
        if (daoFactory == null)
            daoFactory = new DaoFactory();
        return daoFactory;
    }

    public UserDao getUserDao(){
        return userDao;
    }
}
